(function () {
    var consoleLog = console.log;
    var consoleError = console.error;
    
    var Ajax = {
        // datat应为'a=a1&b=b1'这种字符串格式，在jq里如果data为对象会自动将对象转成这种字符串格式
        post: function (url, data, fn) {
            consoleLog(data)
            var xhr = new XMLHttpRequest();
            xhr.open("POST", url, true);
            // 添加http头，发送信息至服务器时内容编码类型
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {
                    fn && fn.call(this, xhr.responseText);
                }
            };
            xhr.send(data);
        }
    }
    /**
     * 随机字符串
     */
    var randomString = function (len) {
        len = len || 32;
        var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        var maxPos = $chars.length;
        var pwd = '';
        for (i = 0; i < len; i++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    }
    if (!localStorage.getItem("_did")) {
        localStorage.setItem("_did", randomString())
    }
    /**
     * 上传日志
     */
    var uploadLog = function (data) {
        Ajax.post("http://logService2.xiaohuzi.net/logUpload", data)
    }
    /**
     * 生成日志
     */
    var getlog = function (content,type) {
        if (typeof content == "object") {
            content = JSON.stringify(content);
        }

        return "href="+location.href+"&logType=" +type + "&_did=" + localStorage.getItem("_did") + "&localStorage=" + JSON.stringify(localStorage) + "&sessionStorage=" + JSON.stringify(sessionStorage) + "&cookie=" + document.cookie + "&userAgent=" + navigator.userAgent + "&content=" + content;
    }
    /**
     * 重写输出方法
     */
    window.uplog=function  () {
        try {
            var content = [].slice.call(arguments).join("");
            var log = getlog(content,"log")
            uploadLog(log)
        } catch (error) {
            uploadLog(getlog(error),"error")
        }

    };
    console.log = function () {
        try {
            var content = [].slice.call(arguments).join("");
            var log = getlog(content,"log")
            consoleLog(arguments);
            uploadLog(log)
        } catch (error) {
            uploadLog(getlog(error),"error")
        }

    };
    console.error = function (e) {
        try {
            var content = e
            var log = getlog(content,"error")
            consoleError(arguments);
            uploadLog(log)
        } catch (error) {
            uploadLog(getlog(error),"error")
        }

    };

}
)()